package edu.nf.vrs.app.controller;

import cn.hutool.core.bean.BeanUtil;
import edu.nf.reservation.dto.ReservationDTO;
import edu.nf.reservation.inf.ReservationService;
import edu.nf.vrs.app.vo.ReservationVO;
import edu.nf.vrs.app.vo.ResultVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Author: @Wai
 * Date: 2022-4-22
 */
@Tag(name = "ReservationController", description = "预约服务接口")
@RestController
@RequestMapping("/api/v1")
public class ReservationController extends BaseController{

    @DubboReference(interfaceClass = ReservationService.class, version="1.0.0",check = false)
    private ReservationService reservationService ;

    @GetMapping("/reservations")
    @Operation(summary="查询个人预约记录", method = "GET")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "返回个人预约记录"),
            @ApiResponse(responseCode = "500", description = "服务异常", content = @Content)
    })
    public ResultVO<List<ReservationVO>> listReservation(@RequestAttribute("uid") Integer uid) {
        List<ReservationDTO> dtoList = reservationService.listReservationByUserId(uid);
        List<ReservationVO> voList = BeanUtil.copyToList(dtoList, ReservationVO.class);
        return success(voList);
    }

    @PostMapping("/reservation")
    @Operation(summary="创建预约", method = "POST")
    @Parameters({
            @Parameter(name="reservationVO", description="预约信息", required=true),
            @Parameter(name="nid", description="针次ID", required=true),
            @Parameter(name="pid", description="接种时段ID", required=true)
    })
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "创建成功"),
            @ApiResponse(responseCode = "500", description = "创建失败", content = @Content)
    })
    public ResultVO addReservation(ReservationVO reservationVO, Integer nid, Integer pid, @RequestAttribute("uid") Integer uid) throws Exception {
        ReservationDTO reservationDTO = BeanUtil.copyProperties(reservationVO, ReservationDTO.class);
        reservationDTO.setUid(uid);
        reservationDTO.setNid(nid);
        reservationDTO.setPid(pid);
        //保存
        reservationService.addReservation(reservationDTO);
        return success();
    }
}
